package cn.szabadsag.product.controller;

import cn.szabadsag.product.domain.Product;
import cn.szabadsag.product.query.ProductQuery;
import cn.szabadsag.product.service.IProductService;
import cn.szabadsag.utils.AjaxResult;
import cn.szabadsag.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;


    /**
     * 根据id查询单个宠物信息
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public Product queryPage(@PathVariable Long id){
        System.out.println("前端传回的id:" + id);
        return productService.loadByIdDetail(id);
    }


    /**
     * 高级查询+分页
     * @param productQuery
     * @return
     */
    @PostMapping
    public QueryResult<Product> queryPage(@RequestBody ProductQuery productQuery){
        return productService.queryPage(productQuery);
    }

    /**
     * 宠物领养下架
     * @param list
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> list){
        try {
            productService.offsale(list);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    /**
     * 宠物领养上架
     * @param list
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> list){
        try {
            productService.onsale(list);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    /**
     * 新增宠物领养
     */
    @PutMapping
    public AjaxResult save(@RequestBody Product product){
        try {
            if(product.getId() == null){
                System.out.println("服务项目具体数据：" + product);
                productService.save(product);
            }else {
                productService.update(product);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }



}
